package cat.joronya.supermanager.service;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.util.Log;
import cat.joronya.supermanager.Supermanager;

/**
 * Exposa l'API de metodes que podem cridar a la
 * web.
 * @author pol
 *
 */
public class SupermanagerServiceHelper 
{
	private static final String TAG = "supermanager";
	
	private static SupermanagerServiceHelper singleton = null;
	
	public static SupermanagerServiceHelper getInstance(Context context)
	{
		if(singleton == null)
			singleton = new SupermanagerServiceHelper(context);
		
		return singleton;
	}
	
	private Context context;
	private String username;
	private String password;
	
	public SupermanagerServiceHelper(Context context)
	{
		// el necessitem per crear els intents i per les prefs.
		this.context = context;
		
		// mirem si tenim user/password a les preferences
   		SharedPreferences settings = 
			PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
   		username = settings.getString(Supermanager.PREFS_USERNAME, null);
   		password = settings.getString(Supermanager.PREFS_PASSWORD, null);
	}
	
	/**
	 * Fem login inicial, forçant l'us del user i password passats.
	 * @param username
	 * @param password
	 * @param handler
	 */
	public void login(String username, String password, Handler handler)
	{
		Log.d(TAG,"SupermanagerServiceHelper.login('"+username+"','"+password+"')");
		
		// guardem username i password al helper per proximes cride
		// si s'ha mort el service i cal reinicia la sessio
		this.username = username;
		this.password = password;
		
		// preparem intent
		SupermanagerIntent intent = 
			new SupermanagerIntent(SupermanagerIntent.ACTION_LOGIN,
					Supermanager.Home.CONTENT_URI, context, handler);
		intent.putExtra("cat.joronya.supermanager.login.username", username);
		intent.putExtra("cat.joronya.supermanager.login.password", password);
		
		// executem l'intent al service
		context.startService(intent);
	}

	/**
	 * Crida al service per updatar tots els apartats de dades 
	 * del supermanager. Les crida sequits i el intent service ja 
	 * les apila i serveix una a una.
	 * @param handler
	 */
	public void fullUpdate(Handler handler, boolean showNotifications)
	{
		Log.d(TAG,"SupermanagerServiceHelper.fullUpdate()");
		
		// preparem intent
		SupermanagerIntent intent = 
			new SupermanagerIntent(SupermanagerIntent.ACTION_SYNC,
					Supermanager.Home.CONTENT_URI, context, handler);
		intent.putExtra("cat.joronya.supermanager.login.username", username);
		intent.putExtra("cat.joronya.supermanager.login.password", password);
		intent.putExtra("cat.joronya.supermanager.fullupdate.shownotifications", showNotifications);
		
		// executem l'intent al service
		context.startService(intent);
	}
	
	/**
	 * Crida al service per recuperar de la web les lligues privades.
	 */
	public void getPrivateLeagues(Handler handler)
	{
		Log.d(TAG,"SupermanagerServiceHelper.getPrivateLeagues()");
		
		// preparem intent
		SupermanagerIntent intent = 
			new SupermanagerIntent(Intent.ACTION_GET_CONTENT,
				Supermanager.PrivateLeague.CONTENT_URI, context, handler);
		intent.putExtra("cat.joronya.supermanager.login.username", username);
		intent.putExtra("cat.joronya.supermanager.login.password", password);
		
		// executem l'intent al service
		context.startService(intent);
	}
	
	/**
	 * Crida al service per recuperar de la web les dades del mercat.
	 */
	public void getMarket(Handler handler)
	{
		Log.d(TAG,"SupermanagerServiceHelper.getMarket()");
		
		// preparem intent
		SupermanagerIntent intent = 
			new SupermanagerIntent(Intent.ACTION_GET_CONTENT,
				Supermanager.Market.CONTENT_URI, context, handler);
		intent.putExtra("cat.joronya.supermanager.login.username", username);
		intent.putExtra("cat.joronya.supermanager.login.password", password);
		
		// executem l'intent al service
		context.startService(intent);		
	}

	/**
	 * Crida al service per recuperar de la web els equips.
	 */
	public void getMyTeams(Handler handler)
	{
		Log.d(TAG,"SupermanagerServiceHelper.getMyTeams()");
		
		// preparem intent
		SupermanagerIntent intent = 
			new SupermanagerIntent(Intent.ACTION_GET_CONTENT,
				Supermanager.MyTeam.CONTENT_URI, context, handler);
		intent.putExtra("cat.joronya.supermanager.login.username", username);
		intent.putExtra("cat.joronya.supermanager.login.password", password);
		
		// executem l'intent al service
		context.startService(intent);
	}

	/**
	 * Crida al service per recuperar de la web el detall de un equip.
	 * @param handler
	 * @param idequ
	 * @param name
	 */
	public void getMyTeamDetail(Handler handler, String idequ )
	{
		Log.d(TAG,"SupermanagerServiceHelper.getMyTeamDetail('"+idequ+"')");
		
		// preparem intent
		SupermanagerIntent intent = 
			new SupermanagerIntent(Intent.ACTION_GET_CONTENT,
				Supermanager.MyTeamDetail.CONTENT_URI, context, handler);
		intent.putExtra("cat.joronya.supermanager.login.username", username);
		intent.putExtra("cat.joronya.supermanager.login.password", password);
		intent.putExtra("cat.joronya.supermanager.myteamdetail.idequ", idequ);
		
		// executem l'intent al service
		context.startService(intent);
	}
	
	/**
	 * Crida al service per resetejar els canvis realitzats.
	 * @param handler
	 */
	public void resetChanges(Handler handler, String idequ)
	{
		Log.d(TAG,"SupermanagerServiceHelper.resetChanges('"+idequ+"')");
		
		// preparem intent
		SupermanagerIntent intent = 
			new SupermanagerIntent(Supermanager.ACTION_RESET_CHANGES,
				Supermanager.MyTeamDetail.CONTENT_URI, context, handler);
		intent.putExtra("cat.joronya.supermanager.login.username", username);
		intent.putExtra("cat.joronya.supermanager.login.password", password);
		intent.putExtra("cat.joronya.supermanager.myteamdetail.idequ", idequ);
		
		// executem l'intent al service
		context.startService(intent);
	}
	
	/**
	 * Crida al service per vendre un jugador.
	 * @param handler
	 */
	public void sellPlayer(Handler handler, String idequ, String position)
	{
		Log.d(TAG,"SupermanagerServiceHelper.sellPlayer('"+idequ+"','"+position+"')");
		
		// preparem intent
		SupermanagerIntent intent = 
			new SupermanagerIntent(Supermanager.ACTION_SELL_PLAYER,
				Supermanager.MyTeamDetail.CONTENT_URI, context, handler);
		intent.putExtra("cat.joronya.supermanager.login.username", username);
		intent.putExtra("cat.joronya.supermanager.login.password", password);
		intent.putExtra("cat.joronya.supermanager.myteamdetail.idequ", idequ);
		intent.putExtra("cat.joronya.supermanager.myteamdetail.position", position);
		
		// executem l'intent al service
		context.startService(intent);
	}
	
	/**
	 * Crida al service per cancelar un canvi d'un jugador.
	 * @param handler
	 */
	public void cancelChange(Handler handler, String idequ, String position)
	{
		Log.d(TAG,"SupermanagerServiceHelper.cancelChange('"+idequ+"','"+position+"')");
		
		// preparem intent
		SupermanagerIntent intent = 
			new SupermanagerIntent(Supermanager.ACTION_CANCEL_PLAYER,
				Supermanager.MyTeamDetail.CONTENT_URI, context, handler);
		intent.putExtra("cat.joronya.supermanager.login.username", username);
		intent.putExtra("cat.joronya.supermanager.login.password", password);
		intent.putExtra("cat.joronya.supermanager.myteamdetail.idequ", idequ);
		intent.putExtra("cat.joronya.supermanager.myteamdetail.position", position);
		
		// executem l'intent al service
		context.startService(intent);
	}

	/**
	 * Crida al service per parserjar els players a mostrar al comprar un jugador.
	 * @param handler
	 */
	public void buyMarketPlayers(Handler handler, String idequ, String position)
	{
		Log.d(TAG,"SupermanagerServiceHelper.buyMarketPlayers('"+idequ+"','"+position+"')");
		
		// preparem intent
		SupermanagerIntent intent = 
			new SupermanagerIntent(Supermanager.ACTION_GET_BUY_PLAYERS,
				Supermanager.Market.CONTENT_URI, context, handler);
		intent.putExtra("cat.joronya.supermanager.login.username", username);
		intent.putExtra("cat.joronya.supermanager.login.password", password);
		intent.putExtra("cat.joronya.supermanager.market.idequ", idequ);
		intent.putExtra("cat.joronya.supermanager.market.position", position);
		
		// executem l'intent al service
		context.startService(intent);
	}

	/**
	 * Crida al service per comprar jugador.
	 * @param handler
	 */
	public void buyMyTeamDetailPlayer(Handler handler, String idequ, String position, String idjug)
	{
		Log.d(TAG,"SupermanagerServiceHelper.buyMyTeamDetailPlayer('"+idequ+"','"+position+"','"+idjug+"')");
		
		// preparem intent
		SupermanagerIntent intent = 
			new SupermanagerIntent(Supermanager.ACTION_BUY_PLAYER,
				Supermanager.MyTeamDetail.CONTENT_URI, context, handler);
		intent.putExtra("cat.joronya.supermanager.login.username", username);
		intent.putExtra("cat.joronya.supermanager.login.password", password);
		intent.putExtra("cat.joronya.supermanager.myteamdetail.idequ", idequ);
		intent.putExtra("cat.joronya.supermanager.myteamdetail.position", position);
		intent.putExtra("cat.joronya.supermanager.myteamdetail.idjug", idjug);
		
		// executem l'intent al service
		context.startService(intent);
	}
	
	/**
	 * Crida al service per cercar private leagues per text.
	 * @param handler
	 */
	public void searchPrivateLeagueWithText(Handler handler, String search)
	{
		Log.d(TAG,"SupermanagerServiceHelper.searchPrivateLeagueWithText('"+search+"')");
		
		// preparem intent
		SupermanagerIntent intent = 
			new SupermanagerIntent(Supermanager.ACTION_SEARCH_PRIVATE_LEAGUE_WITH_TEXT,
				Supermanager.PrivateLeague.CONTENT_URI, context, handler);
		intent.putExtra("cat.joronya.supermanager.login.username", username);
		intent.putExtra("cat.joronya.supermanager.login.password", password);
		intent.putExtra("cat.joronya.supermanager.privateleague.search", search);
		
		// executem l'intent al service
		context.startService(intent);
	}

	/**
	 * Crida al service per cercar private leagues per grup.
	 * @param handler
	 */
	public void searchPrivateLeagueWithGroup(Handler handler, String group)
	{
		Log.d(TAG,"SupermanagerServiceHelper.searchPrivateLeagueWithGroup('"+group+"')");
		
		// preparem intent
		SupermanagerIntent intent = 
			new SupermanagerIntent(Supermanager.ACTION_SEARCH_PRIVATE_LEAGUE_WITH_GROUP,
				Supermanager.PrivateLeague.CONTENT_URI, context, handler);
		intent.putExtra("cat.joronya.supermanager.login.username", username);
		intent.putExtra("cat.joronya.supermanager.login.password", password);
		intent.putExtra("cat.joronya.supermanager.privateleague.group", group);
		
		// executem l'intent al service
		context.startService(intent);
	}

	/**
	 * Crida al service per cercar private leagues per grup.
	 * @param handler
	 */
	public void joinPrivateLeague(Handler handler, String idpri, String ideqs, String clave)
	{
		Log.d(TAG,"SupermanagerServiceHelper.joinPrivateLeague('"+idpri+"','"+ideqs+"')");
		
		// preparem intent
		SupermanagerIntent intent = 
			new SupermanagerIntent(Supermanager.ACTION_JOIN_PRIVATE_LEAGUE,
				Supermanager.PrivateLeague.CONTENT_URI, context, handler);
		intent.putExtra("cat.joronya.supermanager.login.username", username);
		intent.putExtra("cat.joronya.supermanager.login.password", password);
		intent.putExtra("cat.joronya.supermanager.privateleague.idpri", idpri);
		intent.putExtra("cat.joronya.supermanager.privateleague.ideqs", ideqs);
		intent.putExtra("cat.joronya.supermanager.privateleague.clave", clave);
		
		// executem l'intent al service
		context.startService(intent);
	}

	/**
	 * Crida al service per crear una private league.
	 * @param handler
	 */
	public void createPrivateLeague(Handler handler, String nombre, String limite, 
			String limiteeq, String clave, String clave2, String emails)
	{
		Log.d(TAG,"SupermanagerServiceHelper.createPrivateLeague('"+nombre+"',..,'"+clave+"')");
		
		// preparem intent
		SupermanagerIntent intent = 
			new SupermanagerIntent(Supermanager.ACTION_CREATE_PRIVATE_LEAGUE,
				Supermanager.PrivateLeague.CONTENT_URI, context, handler);
		intent.putExtra("cat.joronya.supermanager.login.username", username);
		intent.putExtra("cat.joronya.supermanager.login.password", password);
		intent.putExtra("cat.joronya.supermanager.privateleague.nombre", nombre);
		intent.putExtra("cat.joronya.supermanager.privateleague.limite", limite);
		intent.putExtra("cat.joronya.supermanager.privateleague.limiteeq", limiteeq);
		intent.putExtra("cat.joronya.supermanager.privateleague.clave", clave);
		intent.putExtra("cat.joronya.supermanager.privateleague.clave2", clave2);
		intent.putExtra("cat.joronya.supermanager.privateleague.emails", emails);
		
		// executem l'intent al service
		context.startService(intent);
	}
}
